package com.library;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.library.entities.Account;
import com.library.entities.User;

@Controller
public class SignUpController {
	
	//@RequestMapping(value="/signUp", method=RequestMethod.GET)
	@RequestMapping("signUp")
	public ModelAndView signUp(ModelMap model){
		
		Account account = new Account();
		User user = new User();
		
		model.addAttribute("account", account);
		model.addAttribute("user", user);
		
		return new ModelAndView("signUp", "model", model);
		
	}
	
	@RequestMapping(value="signUp", method=RequestMethod.POST)
	public String signUp(@Valid @ModelAttribute("account")Account account,
			BindingResult resultAccount,
			@Valid @ModelAttribute("user")User user,
			BindingResult resultUser){
		
		if(resultAccount.hasErrors() || resultUser.hasErrors()){;
			return "signUp";
		}
		
		System.out.println(resultAccount + "   " + account);
		System.out.println(resultUser + "   " + user);
		
		return "redirect:books";
	}
	
}

